(function () {
    'use strict';

    angular.module('KingAdmin.pages.pdev.firmware')
        .factory('FirmwareService', FirmwareService);

    /** @ngInject */
    function FirmwareService($resource, $http, toastr,CommonService) {

        var rest = $resource('pdev/firmware/:id', {}, {
            'create': {method: 'POST'},
            'update': {method: 'PUT'},
        });
        
        function getSmartData(param,callback) {
            $resource('pdev/firmware/getSmartData', {}, {
                'query': {method: 'POST'}
            }).query(param,
                function (data,hears) {
                    console.log(data);
                    if(angular.isFunction(callback)) callback(data);
                }, function (error) {
                    toastr.error(error, "提示", {"progressBar": true,});
                });
        }
        function del(param,callback) {
            CommonService.danger('确定删除?', function () {
                rest.delete(param,
                    function (data) {
                        if (data.code == 0) {
                            toastr.success("删除成功！", "提示", {"progressBar": true,});
                        } else {
                            toastr.warning("删除失败！", "提示", {"progressBar": true,});
                        }
                        if(angular.isFunction(callback)) callback(data);
                    }, function (error) {
                        toastr.error(error, "提示", {"progressBar": true,});
                    })
            })
        }
        function save(param,callback) {
            console.log(param);

            CommonService.info('确定保存?',function () {
                if(angular.isDefined(param.id)&&param.id!=null){
                    rest.update(param,
                        function (data) {
                            console.log(data);
                            toastr.success("保存成功","提示",{"progressBar": true,});
                            if(angular.isFunction(callback)) callback(data);
                        }, function (error) {
                            toastr.error(error,"提示",{"progressBar": true,});
                        })
                }else{
                    rest.create(param,
                        function (data) {
                            console.log(data);
                            toastr.success("保存成功","提示",{"progressBar": true,});
                            if(angular.isFunction(callback)) callback(data);
                        }, function (error) {
                            toastr.error(error,"提示",{"progressBar": true,});
                        })
                }
            });
        }
        function getInfo(param,callback) {
            rest.get(param,
                function (data) {
                    console.log(data);
                    if(angular.isFunction(callback)) callback(data);
                }, function (error) {
                    toastr.error(error,"提示",{"progressBar": true,});
                })
        }

        function uploadFirmware(param,callback) {
            // $resource('pdev/firmware/getSmartData', {}, {
            //     'save': {method: 'POST'}
            // }).save(param,
            //     function (data,hears) {
            //         console.log(data);
            //         if(angular.isFunction(callback)) callback(data);callback(data)
            //     }, function (error) {
            //         toastr.error(error, "提示", {"progressBar": true,});
            //     });

            $http({
                method:'POST',
                url:"pdev/firmware/uploadFirm",
                data: param,
                headers: {'Content-Type':undefined},
                transformRequest: angular.identity
            }).success( function ( data ) {
                //上传成功的操作
                // alert("uplaod success");
                console.log(data);
                toastr.success("固件更新上传成功","提示",{"progressBar": true,});

                if(angular.isFunction(callback)) callback(data);
            });
        }

        function upgradeFirm(param,callback) {
            $resource('pdev/firmware/upgradeFirm', param, {
                'post': {method: 'POST'}
            }).post({},
                function (data) {
                    console.log(data);
                    if(angular.isFunction(callback)) callback(data);
                }, function (error) {
                    toastr.error(error, "提示", {"progressBar": true,});
                });
        }
        
        return {
            getSmartData:getSmartData,
            del:del,
            save:save,
            getInfo:getInfo,
            uploadFirmware: uploadFirmware,
            upgradeFirm: upgradeFirm
        };

    }

})();
